package io.tec.cloud.algorithm.c04_v20220613;

import io.tec.cloud.algorithm.c01_v20220601.SortTestUtil;

import java.util.Arrays;

public class Code01_MergeSort {


    public static void mergeSort(int[] array) {

        if (array == null || array.length < 2) {
            return;
        }

        process(array, 0, array.length - 1);
    }

    private static void process(int[] array, int l, int r) {
        // base case
        if (l == r) {
            return;
        }
        int mid = l + ((r - l) >> 1);

        process(array, l, mid);
        process(array, mid + 1, r);
        merge(array, l, mid, r);
    }

    private static void merge(int[] array, int l, int mid, int r) {

        int li = l;
        int ri = mid + 1;

        int[] help = new int[r - l + 1];
        int idx = 0;
        while (li <= mid && ri <= r) {
            if (array[li] <= array[ri]) {
                help[idx++] = array[li++];
            } else {
                help[idx++] = array[ri++];
            }
        }

        while (li <= mid) {
            help[idx++] = array[li++];
        }

        while (ri <= r) {
            help[idx++] = array[ri++];
        }
        for (int i = 0; i < help.length; i++) {
            array[l+i] = help[i];
        }
    }

    public static void main(String[] args) {
        int testTimes = 1000000;
        int maxValue = 500;
        int maxLength = 15;
        System.out.println("Start");
        for (int i = 0; i < testTimes; i++) {
            int[] original = generateArray(maxValue, maxLength);
            int[] copy1 = SortTestUtil.copyArray(original);
            int[] copy2 = SortTestUtil.copyArray(original);
            mergeSort(copy1);
            mergeSort2(copy2);
//            Arrays.sort(copy2);
            if (!SortTestUtil.isEq(copy1, copy2)) {
                SortTestUtil.p(original);
                System.out.println("error");
                break;
            }
        }

        System.out.println("end");
    }

    private static int[] generateArray(int maxValue, int maxLength) {
        int l = (int) (Math.random() * maxLength);
        int[] arr = new int[l];
        for (int i = 0; i < l; i++) {
            arr[i] = (int) (Math.random() * maxValue);
        }
        return arr;
    }

    public static void mergeSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        int N = arr.length;
        int mergeSize = 1;
        while (mergeSize < N) {
            int L = 0;
            while (L < N) {
                if (mergeSize >= N - L) {
                    break;
                }
                int mid = L + mergeSize - 1;
                int R = mid + Math.min(mergeSize, N - mid - 1);
                merge2(arr, L, mid, R);
                L = R + 1;
            }
            // 防止溢出
            if (mergeSize > N / 2) {
                break;
            }
            mergeSize <<= 1;
        }
    }

    private static void merge2(int[] arr, int l, int mid, int r) {
        int[] helper = new int[r - l + 1];
        int idx = 0;
        int li = l;
        int ri = mid + 1;
        while (li <= mid && ri <= r) {
            helper[idx++] = arr[li] <= arr[ri] ? arr[li++] : arr[ri++];
        }
        while (li <= mid) {
            helper[idx++] = arr[li++];
        }
        while (ri <= r) {
            helper[idx++] = arr[ri++];
        }
        for (int i = 0; i < helper.length; i++) {
            arr[l + i] = helper[i];
        }
    }
}
